package com.haitaoit.chengquan.module.home.adapter.parts;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.module.home.model.TitleModel;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by Administrator on 2016/10/24.
 */
public class PartsDownAdapter extends BaseAdapter {
    private Context          context;
    //    private BackCall backCall;
    private List<TitleModel> list;

    //    public void setBackCall(BackCall backCall) {
    //        this.backCall = backCall;
    //    }

    public PartsDownAdapter(Context context) {
        this.context = context;
    }

    public List<TitleModel> getList() {
        return list;
    }

    public void setList(List<TitleModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.textview_city, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.leranlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.get(position).isSelect()) {
                    holder.leranlayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_corner_back3));
                    list.get(position).setSelect(!list.get(position).isSelect());
                    holder.image.setVisibility(View.GONE);
                    holder.text.setTextColor(parent.getResources().getColor(R.color.black));
                } else {
                    holder.leranlayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_corner_appcolor_border));
                    list.get(position).setSelect(!list.get(position).isSelect());
                    holder.image.setVisibility(View.VISIBLE);
                    holder.text.setTextColor(parent.getResources().getColor(R.color.app_color));

                }

                //                PartsDownAdapter.this.backCall.deal(R.id.text, position);
            }
        });

        holder.text.setText(list.get(position).getName());
        if (list.get(position).isSelect()) {
            holder.leranlayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_corner_appcolor_border));
            holder.image.setVisibility(View.VISIBLE);
            holder.text.setTextColor(parent.getResources().getColor(R.color.app_color));
        } else {
            holder.leranlayout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_corner_back3));
            holder.image.setVisibility(View.GONE);
            holder.text.setTextColor(parent.getResources().getColor(R.color.black));
        }


        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.text)
        TextView     text;
        @ViewInject(R.id.leranlayout)
        LinearLayout leranlayout;
        @ViewInject(R.id.image)
        ImageView    image;

    }
}
